<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:t="urn:schemas-microsoft-com:time"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"  
  xmlns:user="http://www.voronenko.com/scripting"   

  xmlns="http://www.w3.org/2005/Atom" 
  xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" 
  xmlns:gd="http://schemas.google.com/g/2005" 
  xmlns:thr="http://purl.org/syndication/thread/1.0"

  xmlns:config="http://www.voronenko.com/config"   


  exclude-result-prefixes="xsl"
  
  
  >

<xsl:output 
  method="xml" 
  encoding="UTF-8"    
  omit-xml-declaration="no"     
  indent="yes"
  />

<xsl:variable name="config" select="document('lj2blogger.xsl.xml')"/>



<xsl:template name="replace_strings">
  <xsl:param name="text" />
  <xsl:param name="search"
      select="$config/*/config:string-replacement/config:search" />
  <xsl:variable name="replaced_text">
    <xsl:call-template name="replace-substring">
      <xsl:with-param name="text" select="$text" />
      <xsl:with-param name="from" select="$search[1]/config:find" />
      <xsl:with-param name="to" select="$search[1]/config:replace" />
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$search[2]">
      <xsl:call-template name="replace_strings">
        <xsl:with-param name="text" select="$replaced_text" />
        <xsl:with-param name="search" select="$search[position() > 1]" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$replaced_text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="replace-substring">
   <xsl:param name="text" select="."/>
   <xsl:param name="from" select="/.."/>
   <xsl:param name="to" select="/.."/>
   <xsl:choose>
   <xsl:when test="contains($text, $from)">
      <xsl:value-of select="substring-before($text, $from)"/><xsl:value-of select="$to"/><xsl:call-template name="replace-substring"><xsl:with-param name="text" select="substring-after($text,$from)"/><xsl:with-param name="from" select="$from"/><xsl:with-param name="to" select="$to"/></xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
   	<xsl:value-of select="$text"/>
   </xsl:otherwise>
   </xsl:choose>
</xsl:template>


<xsl:template name="match-tags">
  <xsl:param name="text" />
  <xsl:for-each select="$config/*/config:tags-matching/config:match">
    <xsl:choose>
      <xsl:when test="contains($text, config:find/node())">
        <category scheme="http://www.blogger.com/atom/ns#" term="{config:tag/text()}" />   
      </xsl:when>
      <xsl:otherwise>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>


<xsl:template match="/">
  <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="livejournal" >
  <feed 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/2005/Atom" 
    xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" 
    xmlns:gd="http://schemas.google.com/g/2005" 
    xmlns:thr="http://purl.org/syndication/thread/1.0">    

    <xsl:copy-of select="$config/*/config:authorinfo/*"/>

   <generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>

   <xsl:apply-templates select="entry"/>

   </feed>
</xsl:template>


<xsl:template match="entry">
  <entry>
    <id>lj2blogger-xsl-<xsl:value-of select="itemid"/></id> 
    <published><xsl:call-template name="replace-substring"><xsl:with-param name="text" select="eventtime"/><xsl:with-param name="from" select="' '"/><xsl:with-param name="to" select="'T'"/></xsl:call-template>+02:00</published> 
    <updated><xsl:call-template name="replace-substring"><xsl:with-param name="text" select="logtime"/><xsl:with-param name="from" select="' '"/><xsl:with-param name="to" select="'T'"/></xsl:call-template>+02:00</updated> 

    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/blogger/2008/kind#post" /> 
    <xsl:call-template name="match-tags">
      <xsl:with-param name="text" select="event/node()"/>
    </xsl:call-template>
    <title type="text"><xsl:apply-templates select="subject/node()"/></title> 
    <content type="html">
      <xsl:apply-templates select="event/node()"/>
     </content> 

    <xsl:copy-of select="$config/*/config:authorinfo/*"/>

    <thr:total>0</thr:total> 
  </entry>
</xsl:template>

<xsl:template match="text()">
   <xsl:call-template name="replace_strings">
     <xsl:with-param name="text" select="."/>
   </xsl:call-template>
</xsl:template>


    
</xsl:stylesheet>